'use strict';
const BaseController = require('./base');
const serviceName = 'user';
// 继承baseController 并且指定serviceName
class RouterController extends BaseController {
  constructor(ctx) {
    super(ctx, serviceName);
  }

  getListQuery(query) {
    let where = {}
    if (query.dept_id) {
        where.dept_id = query.dept_id
    }
    return where
  }

  async login() {
    const { ctx } = this;
    const { phone, password } = ctx.request.body;
    const res = await ctx.service[this.serviceName].login(phone, password);
    this.ctx.success(res);
  }

  async getUserInfo() {
    const { ctx } = this;
    const { userId } = ctx.query;
    const res = await ctx.service[this.serviceName].getUserInfo(userId);
    this.ctx.success(res);
  }

  async register() {
    const { ctx } = this;
    const data = ctx.request.body;
    const res = await ctx.service[this.serviceName].register(data);
    this.ctx.success(res);
  }

  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const data = ctx.request.body;
    const res = await ctx.service[this.serviceName].update(id, data);
    this.ctx.success(res);
  }

  async updatePassword() {
    const { ctx } = this;
    const { id, oldPassword, newPassword } = ctx.request.body;

    // 验证参数
    if (!oldPassword || !newPassword) {
      return this.ctx.error('旧密码和新密码不能为空');
    }

    // 验证新密码长度
    if (newPassword.length < 6) {
      return this.ctx.error('新密码长度不能小于6位');
    }

    const res = await ctx.service[this.serviceName].updatePassword(id, oldPassword, newPassword);
    this.ctx.success(res);
  }
}
module.exports = RouterController;
